/**
 * Created by vincent on 2017/9/12.
 */
moduleStudyMap.controller("EditStudyMapController", function ($$CambioConfig, $scope, $log, $state, LoadingService, UploadFileService,
                                                              ConstantService, DialogService, StudyMapResource) {
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        $scope.editingStudyMap = angular.copy($scope.currentStudyMap);
    });

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.uploadImage = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.editingStudyMap.picture = o.id;
            });
        }
    };

    //
    $scope.uploadSuperImage = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.editingStudyMap.skin.superImage = o.id;
            });
        }
    };

    $scope.submit = function () {
        if ($scope.editSMForm.$valid) {
            //LoadingService.show();
            StudyMapResource.update($scope.editingStudyMap, function (studyMapResult) {
                angular.copy(studyMapResult, $scope.currentStudyMap);
                $log.debug("学习地图更新结果", studyMapResult);
                DialogService.close();
                //LoadingService.hide();
                Alert.success("学习地图更新成功！");
            });
        }
    };

});